package com.duan.test.utils.SendMails;

import java.io.File;
import java.util.List;
import java.util.Properties;
import java.util.UUID;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SimpleMailSender implements AbstractMailSender {
	private Session session;
	private Message message;
	private Multipart multipart; //java mail 中用于存放不同部分邮件内容的容器

	public SimpleMailSender() {
		multipart = new MimeMultipart();
	}
	
	@SuppressWarnings("static-access")
	@Override
	public void sendMail() throws MessagingException {
		//设置邮件内容
		message.setContent(multipart);
		//发送邮件
		Transport tran = session.getTransport();
		tran.send(message);
		tran.close();
	}

	@Override
	public void setTo(List<String> tos) {
		try {
			Address[] to = new Address[tos.size()];
			for (int i = 0; i < tos.size(); i++) {
				to[i] = new InternetAddress(tos.get(i));
			}
			message.setRecipients(RecipientType.TO, to);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setCc(List<String> ccs) {
		try {
			Address[] cc = new Address[ccs.size()];
			for (int i = 0; i < ccs.size(); i++) {
				cc[i] = new InternetAddress(ccs.get(i));
			}
			message.setRecipients(RecipientType.CC, cc);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setSubject(String subject) {
		try {
			message.setSubject(subject);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void addContent(String content) {
		try {
			BodyPart bodyPart = new MimeBodyPart();
			bodyPart.setText(content);
			multipart.addBodyPart(bodyPart);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void addAttach(File attach, String header) {
		try {
			BodyPart bodyPart = new MimeBodyPart();
			DataSource dataSource = new FileDataSource(attach);
			bodyPart.setDataHandler(new DataHandler(dataSource));
			bodyPart.setFileName(attach.getName());
			if(header != null){
				bodyPart.setHeader("Content-ID", "<" + header + ">");
			}
			multipart.addBodyPart(bodyPart);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void addAttach(File attach) {
		addAttach(attach, null);
	}

	@Override
	public void addHtml(String html) {
		try {
			BodyPart bodyPart = new MimeBodyPart();
			bodyPart.setContent(html, "text/html;charset=utf8");
			multipart.addBodyPart(bodyPart);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void addImage(File image){
		try {
			String header = UUID.randomUUID().toString();
			String img = "<img src=\"cid:" + header + "\">";
			addHtml(img);
			addAttach(image, header);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void createMail(Properties props, Authenticator authenticator) {
		session = Session.getInstance(props, authenticator);
		message = new MimeMessage(session);
	}
	@Override
	public void createMail(Properties props) {
		session = Session.getInstance(props);
		message = new MimeMessage(session);
	}
	@Override
	public void setFrom (String internetAddress) {
		try {
			message.setFrom(new InternetAddress(internetAddress));
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
